{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "NUu8Whb-pFQb"
   },
   "source": [
    "## Setup and Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "GGUMoRoLpAPm"
   },
   "outputs": [],
   "source": [
    "!pip install -Uq trackio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "S5tUKE8cpp2l"
   },
   "outputs": [],
   "source": [
    "import trackio"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "n5FU2Dfap0G3"
   },
   "source": [
    "## Initialize a project\n",
    "\n",
    "We will initialize a project, which will hold a collection of runs. You can consider runs to be each individual experiment.\n",
    "\n",
    "> **Note:** You need to have your Hugging Face token accessible (e.g., stored in your environment as `HF_TOKEN`).\n",
    "> You can generate or copy your token from [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "5qzsH21Hp3WJ"
   },
   "outputs": [],
   "source": [
    "experiment_1 = trackio.init(\n",
    "    project=\"quickstart\", name=\"experiment_1\", space_id=\"quickstart-trackio\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ErSvi3vds5Ga"
   },
   "source": [
    "After we have initialized a run for a project, it is time we log some metrics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "IKbC9Z68q37i"
   },
   "outputs": [],
   "source": [
    "for idx in range(100):\n",
    "    experiment_1.log(\n",
    "        {\n",
    "            \"idx\": idx,\n",
    "            \"loss\": 100 - idx,\n",
    "        }\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "LdYuRsC9tcqt"
   },
   "source": [
    "After uploading the metrics let's finish this experiment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "YlE7aQoMthfB"
   },
   "outputs": [],
   "source": [
    "experiment_1.finish()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "r_cPwOBLtlUW"
   },
   "source": [
    "## Adding configuration\n",
    "\n",
    "Each experiment we run has different configurations. Here we will tag configs to runs.\n",
    "\n",
    "You can also use the `config` attribute when you run the experiment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "f0rp8fsqrXh2"
   },
   "outputs": [],
   "source": [
    "config = {\n",
    "    \"lr\": 0.01,\n",
    "}\n",
    "experiment_2 = trackio.init(\n",
    "    project=\"quickstart\",\n",
    "    name=\"experiment_2\",\n",
    "    space_id=\"quickstart-trackio\",\n",
    "    config=config,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "T2JjMQiPuEId"
   },
   "outputs": [],
   "source": [
    "experiment_2.config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "SH7RbXySt5_s"
   },
   "outputs": [],
   "source": [
    "for idx in range(100):\n",
    "    experiment_2.log(\n",
    "        {\n",
    "            \"idx\": idx,\n",
    "            \"loss\": 100 - idx * experiment_2.config[\"lr\"],  # We use the config here.\n",
    "        }\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "4V936sOeubef"
   },
   "outputs": [],
   "source": [
    "experiment_2.finish()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "sbSpcYYiud04"
   },
   "source": [
    "## Wrapping it all together"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "I7Hfo4DXpomp"
   },
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "num_experiments = 5\n",
    "epochs = 100\n",
    "\n",
    "for idx in range(num_experiments):\n",
    "    experiment = trackio.init(\n",
    "        project=\"quickstart\", name=f\"experiment_{idx}\", space_id=\"quickstart-trackio\"\n",
    "    )\n",
    "\n",
    "    for epoch in range(epochs):\n",
    "        train_loss = random.uniform(0.2, 1.0)\n",
    "        train_acc = random.uniform(0.6, 0.95)\n",
    "\n",
    "        val_loss = train_loss - random.uniform(0.01, 0.1)\n",
    "        val_acc = train_acc + random.uniform(0.01, 0.05)\n",
    "\n",
    "        experiment.log(\n",
    "            {\n",
    "                \"epoch\": epoch,\n",
    "                \"train_loss\": train_loss,\n",
    "                \"train_accuracy\": train_acc,\n",
    "                \"val_loss\": val_loss,\n",
    "                \"val_accuracy\": val_acc,\n",
    "            }\n",
    "        )\n",
    "\n",
    "    experiment.finish()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "oDqjsYEJpIdN"
   },
   "source": [
    "##"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "authorship_tag": "ABX9TyPtqAbSpD/QqxOkuK4BIqjP",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}